import DataCenter from "../DataCenter";

export const enum MINO_TYPE {
    I = 0,
    O = 1,
    T = 2,
    L = 3,
    J = 4,
    S = 5,
    Z = 6,
}

export class Mino {

    get unitType(): MINO_TYPE {
        return MINO_TYPE.I;
    }

    minoList: cc.Node[] = [];

    willDie: boolean = false;

    center: cc.Vec2 = cc.v2();

    // angle: 0,1,2,3
    angle: number = 0;

    setAngle(angle: number) {
        this.angle = angle;
    }

    getPos(angle: number): cc.Vec2[] { return []; }

    createMino(count: number, Template: cc.Node, parent: cc.Node, color: cc.Color) {
        const width = DataCenter.Width;
        for (let i = 0; i < count; i++) {
            const node = cc.instantiate(Template);

            node.active = true;
            node.width = width;
            node.height = width;
            node.color = color;
            
            parent.addChild(node);
            this.minoList.push(node);
        }
    }

    getPosIndex(angle: number, center: cc.Vec2): cc.Vec2[] {
        const posList = this.getPos(angle);
        const posIndex: cc.Vec2[] = [];

        for (let i = 0; i < posList.length; i++) {
            const pos = posList[i];

            posIndex.push(cc.v2(pos.x + center.x, pos.y + center.y));
        }
        return posIndex;
    }

    updatePos() {
        const list = this.minoList;
        const posIndex = this.getPosIndex(this.angle, this.center);

        const w = DataCenter.Width;
        const h = w;
        const dx = DataCenter.SPACING;
        const dy = dx;

        for (let i = 0, len = list.length; i < len; i++) {
            const node = list[i];
            const pos = posIndex[i];

            const x = pos.x;
            const y = pos.y;

            const posX = (w + dx) * x;
            const posY = (h + dy) * y;

            node.setPosition(posX, posY);
        }
    }


    /**
     * 顺时针旋转
     */
    getNextAngle() {
        const angle = this.angle + 1;
        return angle % 4;
    }

    down(step: number = 1) {
        this.center.y -= step;
    }

    left() {
        this.center.x--;
    }

    right() {
        this.center.x++;
    }
}
